import React from 'react';
import './scheduleSetting.less';
import {browserHistory} from 'react-router';
import { Button, Table, Modal, Pagination, message} from 'antd';
import $ from 'jquery';
import config from '../../../config/config';
const allRouter=config.router;
const token = sessionStorage.getItem('token');
class ScheduleSetting extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            schedulingsList:[],//排版列表
            pagesize: 12,// 每页显示的页数
            pageindex: 1,// 当前页
            storesCount:0,//门店总数
        }
    }
    componentDidMount() {
        //获取班次列表
        this.getScheduleList();
    }
    //获取班次列表
    getScheduleList() {
        let url=allRouter.patientOrigin+'/sheduling/list';
        console.log(url);
        $.ajax({
            url:url,
            type: "GET",
            async: true,
            headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
            success:data=>{
                console.log(data);
                if(data.code===200){
                    //给列表每一项增加索引
                    if (data.schedulings.length > 0) {
                        data.schedulings.forEach((ele, i) => {
                            ele.index= i + 1;
                            let TimeString= '';
                            ele.workingtime.forEach((item,i)=>{
                                if(item){
                                    if(i>=1){
                                        TimeString += '/';
                                    }
                                    TimeString += item.starttime.slice(0,5)+"-"+item.endtime.slice(0,5)

                                }
                            })
                            ele.schedulingsTimeString = TimeString;
                        });
                    }

                    this.setState({
                        schedulingsList:data.schedulings
                    },()=>{
                        console.log(this.state.schedulingsList);
                    })
                }else{
                    console.log("获取班次列表出错");
                }
            },
            error:err=>{
                console.log(err);
            }
        });
    }
    //分页
    changePage(pageindex) {
        this.setState({
            pageindex
        },);
    }

    //删除班次数据
    deleteSchedule(store) {
        Modal.confirm({
            title: '删除班次',
            content: '确认删除该班次数据吗?',
            cancelText:'取消',
            okText:'确定',
            onCancel: () => { message.info('取消删除该班次数据') },
            onOk: () => {
                //发送Ajax请求
                let url=allRouter.patientOrigin+'/sheduling/delete';
                $.ajax({
                    url:url,
                    type: "POST",
                    async: true,
                    headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
                    data:{
                        id:store._id,
                    },
                    success:data=>{
                        console.log(data);
                        if(data.code==200){
                            message.success('删除成功！', 2);
                            this.getScheduleList();
                        }else{
                            message.info('删除失败');
                        }
                    },
                    error:err=>{
                        console.log(err);
                    }
                });
            }
        })
    }
    //编辑班次信息
    editSchedule(record) {
        browserHistory.push('addSchedule/'+record._id);

    }
    //编辑班次状态信息
    editPatientOriginState(record) {
        let that=this;
        let actualEditState=()=>{
            let url=allRouter.patientOrigin+'/sheduling/update';
            $.ajax({
                url:url,
                type: "POST",
                async: true,
                headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
                data:{
                    id:record._id,
                    state:currentState,
                },
                success:data=>{
                    console.log(data);
                    if(data.code===200){
                        that.getScheduleList();
                    }else{
                        message.info('更改状态错误');
                    }
                },
                error:err=>{
                    console.log(err);
                }
            });
        };
        let currentState=record.state;
        if(currentState==="启用"){
            currentState="禁用";
            Modal.confirm({
                title: '禁用门店',
                className:'confirm-error',
                content: '谨慎操作！在排班管理将失效。是否确定该操作?',
                onCancel: () => { },
                onOk: () => {
                    actualEditState();
                }
            })
        }else if(currentState==="禁用"){
            currentState="启用";
            actualEditState();
        }
    }

    render() {
        const columns = [
            {
                title: '序号',
                dataIndex: 'index',
                width: 50,
            },
            {
                title: '班次名称',
                dataIndex: 'name',
                width: 100,
            }, {
                title: '班次时间',
                dataIndex: 'schedulingsTimeString',
                width: 200,
            },{
                title: '状态',
                dataIndex: 'state',
                width: 50,
                render: (text,record) => <Button onClick={this.editPatientOriginState.bind(this,record)} type={text=='启用'?'primary':text=='禁用'?'danger':''}>{text=='启用'?'已启用，点击可禁用':text=='禁用'?'已禁用，点击可启用':''}</Button>
            }, {
                title: '操作',
                width: 150,
                render: (text, record) => (
                    <div>
                        <Button onClick={this.editSchedule.bind(this, record)} type="primary" >编辑</Button>
                        {/*<Button onClick={this.deleteSchedule.bind(this, record)}  type="danger">删除</Button>*/}
                    </div>
                )
            }
        ];
        let index = this.state.pageindex;
        let size = this.state.pagesize;
        const data = this.state.schedulingsList.slice(size*(index-1),size*index);
        return (
            <div className="container">
                <div className="store-manage-content">
                    {/*增加按钮*/}
                    <div className="storeAdd">
                        <Button onClick={() => { browserHistory.push('addSchedule/0') }}  type="primary"> <img src={require('../../../../images/add.png')} alt=""/>新增</Button>
                    </div>
                    {/*医生挂号费列表 */}
                    <div className='all-roles-table'>
                        <Table
                            rowKey="index"
                            columns={columns}
                            dataSource={data}
                            pagination={false}
                            bordered
                        />
                    </div>
                    {/*分页*/}
                    <div className="pagination">
                        <Pagination showQuickJumper current={this.state.pageindex} total={this.state.schedulingsList.length || 1} defaultPageSize={this.state.pagesize} onChange={this.changePage.bind(this)} />
                    </div>
                </div>
            </div>
        )
    }
}
export default ScheduleSetting;